﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Common;

namespace MockPrjTeam4
{
    public partial class DirectorateEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string popup = @"<script>
            function popup(url, width, height)
            {
                //var width = 500;
                //var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) 
                { 
                    newwin.focus() 
                }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "Mast", popup);
            }

            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            Directorate Dir = DirectorateBiz.Search(id);
            if (!IsPostBack)
            {
                txtDirName.Text = Dir.DirectorateName;
                txtDirShortDes.Text = Dir.ShortDescription;
                DataTable dt = DirectorateBiz.SearchTableDir(id);
                DataRow row;
                if (dt != null)
                {
                    row = dt.Rows[0];
                    txtContactName.Text = row["ContactName"].ToString();
                    txtTypeOfBusiness.Text = row["BusinessName"].ToString();
                    txtSICCode.Text = row["SICCode"].ToString();
                    txtTown.Text = row["TownName"].ToString();
                    txtCounty.Text = row["CountyName"].ToString();
                    txtCountry.Text = row["CountryName"].ToString();
                    hdfTypeOfBusinessId.Value = row["BusinessId"].ToString();
                    hdfAddressId.Value = row["AddressId"].ToString();
                    hdfTownId.Value = row["TownId"].ToString();
                    hdfCountyId.Value = row["CountyId"].ToString();
                    hdfCountryId.Value = row["CountryId"].ToString();
                }
                txtPhoneNumber.Text = Dir.PhoneNumber;
                txtDirFullDes.Text = Dir.FullDescription;
                txtFax.Text = Dir.Fax;
                txtEmail.Text = Dir.Email;
                txtWebAdd.Text = Dir.WebAddress;
                txtCharityNumber.Text = Dir.CharityNumber;
                txtCompanyNumber.Text = Dir.CompanyNumber;
                if (Dir.IsActive == true) chkIsActive.Checked = true;
                else chkIsActive.Checked = false;
                txtAddLine1.Text = Dir.AddressLine1;
                txtAddLine2.Text = Dir.AddressLine2;
                txtAddLine3.Text = Dir.AddressLine3;
                txtPostcode.Text = Dir.PostCode;
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            Directorate Dir = new Directorate();
            Dir.DirectorateName = txtDirName.Text;
            Dir.ShortDescription = txtDirShortDes.Text;
            Dir.AddressId = Methods.ConvertNumber(hdfAddressId.Value);
            Dir.BusinessId = Methods.ConvertNumber(hdfTypeOfBusinessId.Value);
            Dir.PhoneNumber = txtPhoneNumber.Text;
            Dir.TownId = Methods.ConvertNumber(hdfTownId.Value);
            Dir.CountyId = Methods.ConvertNumber(hdfCountyId.Value);
            Dir.CountryId = Methods.ConvertNumber(hdfCountryId.Value);
            if (hdfContact.Value == "")
                Dir.ContactId = -1;
            else
                Dir.ContactId = Methods.ConvertNumber(hdfContact.Value);
            Dir.FullDescription = txtDirFullDes.Text;
            Dir.Fax = txtFax.Text;
            Dir.Email = txtEmail.Text;
            Dir.WebAddress = txtWebAdd.Text;
            Dir.CharityNumber = txtCharityNumber.Text;
            Dir.CompanyNumber = txtCompanyNumber.Text;
            if (chkIsActive.Checked) Dir.IsActive = true;
            else Dir.IsActive = false;
            Dir.PostCode = txtPostcode.Text;
            Dir.AddressLine1 = txtAddLine1.Text;
            Dir.AddressLine2 = txtAddLine2.Text;
            Dir.AddressLine3 = txtAddLine3.Text;

            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            Directorate DirName = DirectorateBiz.SearchDirName(txtDirName.Text);
            if (DirName != null && id != DirName.DirectorateId) 
            {
                lblMessage.Text = txtDirName.Text + " had in DB. Enter again";
            }
            else
            {
                DirectorateBiz.EditDirectorate(Dir, id);
                lblMessage.Text = "You edited " + txtDirName.Text + " successfully";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            int id = Methods.ConvertNumber(Request.QueryString["OrgID"]);
            Response.Redirect(string.Format("EditOrganisation.aspx?ID={0}", id));
        }
    }
}